const express = require('express');
const db = require('../utils/database')
const upload = require('../utils/multer');
const route = express.Router();
route.get('/plant/get', (req, res) => {
    db.query(req.query.sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err })
        } else {
            db.query(req.query.sql2, (err2, data2) => {
                if (err2) {
                    console.log(err2);
                    res.send({ error: 1, msg: err2 })
                } else {
                    res.send({ error: 0, data: data, total: data2[0].total })
                }
            })
        }
    })
})
route.get('/plant/del', (req, res) => {
    console.log(req.query.id)
    const sql = `DELETE FROM ${req.query.t} WHERE ${req.query.tid}=${req.query.id}`
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: '失败' })
        } else {
            res.send({ error: 0, msg: '成功' })
        }
    })
})
route.post('/plant/edit', (req, res) => {
    console.log(req.body.sql)
    db.query(req.body.sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: '失败' })
        } else {
            res.send({ error: 0, msg: '成功' })
        }
    })
})
route.post('/upload', upload.single('file'), function (req, res) {
    console.log(req.file)
    res.send({ code: 200, msg: req.file.filename })
})
route.post('/plant/add', (req, res) => {
    sql2 = `SELECT g_id FROM goods WHERE g_id=(SELECT MAX(g_id) FROM goods)`
    db.query(req.body.sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: '失败' })
        } else {
            db.query(sql2, (err2, data2) => {
                if (err) {
                    res.send({ error: 1, msg: '失败' })
                } else {
                    res.send({ error: 0, msg: '成功', myid: data2 })
                    console.log(data2)
                }
            })
        }
    })
})
route.get('/board/look', (req, res) => {
    const sql = `SELECT * FROM board  WHERE b_id=${req.query.id}`
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err)
            res.send({ error: 1, msg: '失败' })
        } else {
            res.send({ error: 0, data: data })
        }
    })
})
module.exports = route;